﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.IO;

namespace Wallpaper
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
        protected override void OnStart(string[] args)
        {
            try
            {
                string filepath = "c:\\windows\\wallpaper\\";
                
                DayOfWeek day = DateTime.Now.DayOfWeek;

                string filename = day.ToString() + ".bmp";
                filepath += filename;        

                RegistryKey myRegKey = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
                myRegKey.SetValue("TileWallpaper", "0");
                myRegKey.SetValue("WallpaperStyle", "2");
                myRegKey.SetValue("Wallpaper", filepath);
                myRegKey.Close();
                int result = SystemParametersInfo(20, 1, filepath, 0x1 | 0x2);

                this.Stop();
            }
            catch (System.Exception e)
            {
                StreamWriter writer = File.AppendText("c:\\exception.txt");

                writer.WriteLine(e.Message);
                writer.Close();
            }
        }

        protected override void OnStop()
        {
        }
    }
}
